// pages/map/index.js
var mapapi = require('../../commons/libmap.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '门店地图',
    theme: 'light',
    latitude: 22.985136,
    longitude: 113.750965,
    storesCount: 0,
    markers: [],
    points: [],//添加当前定位点，以便全幅展示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.mapCtx = wx.createMapContext('myMap');
    wx.getLocation({
      type: 'gcj02',//wgs84',
      success: (res) => {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        this.setData({ latitude: latitude, longitude: longitude })
        this.showNearbyStores();
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
     * 定位函数
     */
  getCenterLocation3: function () {

    this.mapCtx.getCenterLocation({
      success: function (res) {
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  },
  map_move:function(e){
    /**
    this.mapCtx.getCenterLocation({
      success: (result) => {
        console.log(result.latitude,result.longitude)
      },
      fail: () => {},
      complete: () => {}
    });
     */
  },
  jumpPage:function(e){
    wx.navigateTo({
      url: '/pages/stores/index?loc='+e.currentTarget.dataset.loc,
  })
  },
  marker_tap: function(res){
    console.log(res);
    let lat = '';
    let lng = '';
    let name = '';
    let markerId = res.markerId;
    let markers = this.data.markers;
    console.log(markers)
    for (let item of markers){
      if(item.id == markerId){
        lat = item.latitude;
        lng = item.longitude;
        name = item.callout.content;
        wx.openLocation({
          latitude: lat,
          longitude: lng,
          scale: 18,
          name: name,
          address: item.address,
          success: (result) => {
            console.log("打开地图成功！");
          },
          fail: () => {},
          complete: () => {}
        });
        break;
      }
    }
  },
  showNearbyStores: function(){
    mapapi.getNearbyStores(this.data.latitude+','+this.data.longitude).then(res=>{
      //console.log(res);
      let items = res.data;
      if(res.count>=1){
        let arrs = [];
        for (let item of items){
          arrs.push({
            id: item.ud_id,
            latitude: item.location.lat,
            longitude: item.location.lng,
            alpha: 1,
            title: item.title,
            address: item.address,
            callout: {
              content: item.title,
              color: "#ffffff",
              bgColor: "#1aaba8",
              padding: 7,
              display: 'ALWAYS',
              textAlign: "center"
            }
          });
        }
        let arrs2 = arrs;
        arrs2.push({id: 0,latitude:this.data.latitude,longitude:this.data.longitude,alpha:0,});
        //console.log(arrs);
        this.setData({
          storesCount:res.count,
          markers:arrs,
          points:arrs2,
        });
        wx.showToast({
          title: '发现'+this.data.storesCount+'家门店',
          duration: 1500,
          mask: false,
        });
        this.onShow();
      }else{
        wx.showToast({
          title: '附近发现门店较少',
          duration: 1500,
          mask: false,
        });
        this.showAllStores();
      }      
    }).then(res=>{});

  },
  showAllStores: function(){
    mapapi.getAllStores("东莞",this.data.latitude+','+this.data.longitude).then(res=>{
      let items = res.data;
      let arrs = [];
      for (let item of items){
        arrs.push({
          id: item.ud_id,
          latitude: item.location.lat,
          longitude: item.location.lng,
          alpha: 1,
          title: item.title,
          address: item.address,
          callout: {
            content: item.title,
            display: 'ALWAYS',
            textAlign: "center"
          }
        });
      }
      let arrs2 = arrs;
      arrs2.push({id: 0,latitude:this.data.latitude,longitude:this.data.longitude,alpha:0,});
      //console.log(arrs);
      this.setData({
        storesCount:res.count,
        markers:arrs,
        points:arrs2,
      });
      wx.showToast({
        title: '共有'+this.data.storesCount+'家门店',
        duration: 1500,
        mask: false,
      });
      this.onShow();
    })
  }, 
  getCenterLocation2: function () {
    wx.getLocation({
      type: 'gcj02', // 返回可以用于wx.openLocation的经纬度
      success: (res) => {
        const latitude = res.latitude
        const longitude = res.longitude
        console.log(res.longitude)
        console.log(res.latitude)
        //this.mapCtx=wx.createMapContext('myMap')
        this.mapCtx.moveToLocation({
          latitude: res.latitude,
          longitude: res.longitude,
          success(e) { console.log("移动成功！") }
        });
        //wx.openLocation({latitude,longitude,scale: 18})
      }
    })
  }
})